<?php


namespace app\admin\controller;
use app\common\model\Auth;


use think\Db;

class Index extends Base
{
    public function index()
    {
        $menus = Auth::menu();//print_r($menus);
        //$menus = config('menus.');
        $this->assign('menus', $menus);
        return $this->fetch();
    }

    public function welcome()
    {
        $data=[];
        if($this->admin['role_id']==1){
            if (config('database.type') == 'sqlite') {
                $dbSize = round(filesize(str_replace('sqlite:', '', config('database.dsn'))) / 1024 / 1024, 2);
            } else {
                $query = Db::query(" select round(sum(DATA_LENGTH/1024/1024),2) as data from information_schema.TABLES where TABLE_SCHEMA ='" . (config('database.database')) . "' LIMIT 1;");
                $dbSize = !empty($query[0]['data']) ? $query[0]['data'] : '0.00';
            }
            $domain=model('Domain')->count();
            $logEmail=model('logEmail')->count();
            $logEmailSuc=model('logEmail')->where('status', '=', 1)->count();
            $logEmailError=model('logEmail')->where('status', '=', -1)->count('try_count');
        }else{
            $dbSize='';
            $id = $this->admin['id'];
            $domain=model('Domain')->where('admin_id',$id)->count();
            $logEmail=model('logEmail')->where('admin_id',$id)->count();
            $logEmailSuc=model('logEmail')->where('admin_id',$id)->where('status', '=', 1)->count();
            $logEmailError=model('logEmail')->where('admin_id',$id)->where('status', '=', -1)->count('try_count');
        }
        $data = [
            'domain' => $domain,
            'logEmail' => $logEmail,
            'logEmailSuc' => $logEmailSuc,
            'logEmailError' => $logEmailError,
            'dbSize'=>$dbSize,
        ];

        $this->assign('data', $data);
        return $this->fetch();
    }
    public function persion(){
        
        $id = $this->admin['id'];
        if (request()->isPost()) {
            $param = input('post.');
            $data=[];
            if(!empty($param['nickname']))$data['nickname']=$param['nickname'];
            if(!empty($param['email']))$data['email']=$param['email'];
            if(!empty($param['password'])){
                if($this->admin['username']=='test'){
                    return data_return('测试用户不可修改密码', [], -1);
                }
                $data['password']=sha1($param['password']);
            }
            if($data){
                $res = model('Admin')->where('id',$id)->update($data);
                 data_return('操作成功', $res);
            }else{
                data_return('非法操作', [], -1);
            }
        }else{
            $this->assign('data', model('Admin')->fetch_data($id));
            return $this->fetch();
        }
    }


}